const path = require('path');

// Import the refactored utility functions
const { extractUrlApiKeyFromFile, extractAPIEndpointKeys } = require('./out/utils/apiKeyExtractor');

// Test the refactored functionality
function testRefactoredUtility() {
  console.log('=== 测试重构后的 API 键名提取工具 ===\n');
  
  // Test file with functions and comments
  const testFilePath = path.join(__dirname, 'test-with-functions.js');
  
  console.log('1. 测试 extractUrlApiKeyFromFile 函数:');
  console.log('文件路径:', testFilePath);
  
  try {
    const extractedKeys = extractUrlApiKeyFromFile(testFilePath);
    
    console.log(`\n提取结果: 找到 ${extractedKeys.length} 个有效的 API 端点键名`);
    console.log('提取到的键名:');
    extractedKeys.sort().forEach((key, index) => {
      console.log(`  ${index + 1}. ${key}`);
    });
    
    // 验证是否包含预期的键名
    const expectedKeys = [
      'useSearch.find.pro.list',
      'sm.find.pro.list',
      'sm.command.edit',
      'sm.command.del.smitem',
      'file.find.pro.hList',
      'file.command.add.upload',
      'project.find.pro.list'
    ];
    
    console.log('\n=== 验证预期键名 ===');
    let allFound = true;
    for (const expectedKey of expectedKeys) {
      const found = extractedKeys.includes(expectedKey);
      console.log(`${expectedKey}: ${found ? '✅ 找到' : '❌ 未找到'}`);
      if (!found) {
        allFound = false;
      }
    }
    
    // 验证不应该包含的无效键名
    console.log('\n=== 验证排除的无效键名 ===');
    const invalidKeys = [
      'code', 'msg', 'pageNo', 'pageSize', 'total', 'data',
      'success', 'errors', 'warnings',
      'timeout', 'retries', 'baseURL',
      'version', 'name', 'author'
    ];
    
    let allExcluded = true;
    for (const invalidKey of invalidKeys) {
      const excluded = !extractedKeys.includes(invalidKey);
      console.log(`${invalidKey}: ${excluded ? '✅ 已排除' : '❌ 未排除'}`);
      if (!excluded) {
        allExcluded = false;
      }
    }
    
    console.log(`\n总结:`);
    console.log(`- 预期键名检查: ${allFound ? '✅ 通过' : '❌ 失败'}`);
    console.log(`- 无效键名排除: ${allExcluded ? '✅ 通过' : '❌ 失败'}`);
    console.log(`- 整体测试: ${allFound && allExcluded ? '✅ 成功' : '❌ 失败'}`);
    
    return { success: allFound && allExcluded, extractedKeys };
    
  } catch (error) {
    console.error('测试过程中发生错误:', error);
    return { success: false, error };
  }
}

// 测试 extractAPIEndpointKeys 函数
function testDirectFunction() {
  console.log('\n\n2. 测试 extractAPIEndpointKeys 函数:');
  
  const testFilePath = path.join(__dirname, 'test-with-functions.js');
  
  try {
    const directKeys = extractAPIEndpointKeys(testFilePath);
    console.log(`直接调用结果: 找到 ${directKeys.length} 个 API 端点键名`);
    
    return { success: true, directKeys };
  } catch (error) {
    console.error('直接函数调用测试失败:', error);
    return { success: false, error };
  }
}

// 运行所有测试
function runAllTests() {
  console.log('开始测试重构后的 API 键名提取功能...\n');
  
  const test1 = testRefactoredUtility();
  const test2 = testDirectFunction();
  
  console.log('\n=== 最终测试报告 ===');
  console.log(`extractUrlApiKeyFromFile: ${test1.success ? '✅ 通过' : '❌ 失败'}`);
  console.log(`extractAPIEndpointKeys: ${test2.success ? '✅ 通过' : '❌ 失败'}`);
  
  const overallSuccess = test1.success && test2.success;
  console.log(`总体测试结果: ${overallSuccess ? '✅ 重构成功！' : '❌ 重构需要修复'}`);
  
  return overallSuccess;
}

// 如果直接运行此脚本，则执行测试
if (require.main === module) {
  runAllTests();
}

module.exports = {
  testRefactoredUtility,
  testDirectFunction,
  runAllTests
};